package aoi.movieweb.service;

import aoi.movieweb.model.Movie;
import aoi.movieweb.repository.MovieRepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MovieService {

    private final MovieRepository movieRepository;

    @Autowired
    public MovieService(MovieRepository movieRepository) {
        this.movieRepository = movieRepository;
    }


    public Movie addMovie(Movie movie) {
        return movieRepository.save(movie);
    }


    public List<Movie> getAllMovies() {
        return movieRepository.findAll();
    }


    public Movie findById(Long id) {
        return movieRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Movie not found with id: " + id));
    }


    public Movie updateMovie(Movie movie) {
        return movieRepository.save(movie);
    }


    public void deleteMovie(Long id) {
        movieRepository.deleteById(id);
    }
}
